#WG=`wget`
TxLj="https://ns.ci/secure/uploads/download?hashes=NDQ4MjB8cGFkZA&shareable_link=946"
#TxLj="https://91io.cn/s/PjKaGH7/termux.tar.gz"
#[[ $WG ]] && cd
[ -r /data/data/com.termux/ ] || mkdir -p /data/data/com.termux
ls -ll termux.tar.gz|awk '{print $5}'|grep -qs 64025600 && Ttar=0
#[ -r /data/user/0/com.termux/files/usr/lib/libreadline.so.8 ] || \
[ T0 == T$Ttar ] || wget -O termux.tar.gz $TxLj || curl -o termux.tar.gz "$TxLj"
Ptth=/data/data/com.termux/files/usr/lib
[ -r $Path/libcares.so ] || tar -xvf termux.tar.gz -C /data/data/com.termux/
ls -ll termux.tar.gz|awk '{print $5}'|grep -qs 64025600 || echo "链接失效" || exit 1
[ -r aria2c ] || curl -o aria2c https://ghproxy.com/https://github.com/Hnsyx/biji/raw/main/aria2c
chmod 777 $(pwd)/aria2c

[ -r sudo ] || curl -o sudo https://gitee.com/ewf-9/BiJi/raw/master/sudo
chmod 777 $(pwd)/sudo

[ -r $(pwd)/proot ] || curl -o proot https://gitee.com/ewf-9/BiJi/raw/master/proot
chmod 777 $(pwd)/proot

#[ -e alpine ] || mkdir alpine
HQLJ=`$(pwd)/aria2c -o /dev/aria2 https://91io.cn/s/ZX9ADCy/file.tar.gz|grep https|awk '{print $8}'|cat|sed 's/Segmentation//'`
[ -r /dev/null ] || rm -rf $(pwd)/dev
#echo 'HQ=$HQLJ' HQLJ.txt
#. HQLJ.txt
ls -ll file.tar.gz|awk '{print $5}'|grep -qs 388607799 && La=0 && \
[ L0 == L$La ] || curl "$HQLJ" -o file.tar.gz

[ -e alpine ] || mkdir alpine
if [ -r alpine/boot/init.sh ];then
echo "已解压"
else
[ -r file.tar.gz ] && tar -xvf file.tar.gz -C alpine && Tar=Sbai
[ JSbai == $Tar ] || $(pwd)/sudo tar -xvf file.tar.gz -C alpine
#[[ `ls -l alpine/boot/init.sh|awk '{print $1}'|grep 'rwxrwxrwx'` ]] || sudo chmod -R 777 alpine
fi
grep -qs "bash ql.sh" $(pwd)/.bashrc || Brc="alpine"
if [ alpine == $Brc ];then
cat > $(pwd)/.bashrc <<-EOF
ht=\`ps -ef |grep /ql/build/app.js|grep -v "grep" | wc -l\`
if [ 0 == \$ht ];then
bash ~/ql.sh
fi
EOF
fi
#自启设置
sudo grep -qs init.sh alpine/etc/profile.d/motd.sh || echo -e 'ht=`ps -ef |grep /ql/build/app.js|grep -v "grep" | wc -l`\nif [ 0 == $ht ];then\nnohup bash /boot/init.sh 2>&1 & \nfi' >alpine/etc/profile.d/motd.sh && \
echo "写入成功"

#写入启动文件
cat > ql.sh <<-EOF
#!/bin/bash
#pkg update && pkg install wget && pkg install proot && pkg install ncurses-utils && pkg install pulseaudio
cd .
## unset LD_PRELOAD in case termux-exec is installed
#pulseaudio --start & 
#echo "" &
PWD=/data/user/0/bin.mt.plus/files/term/usr/bin
echo "(以Root 权限正在运行)欢迎来到mt-alpine-ql系统" &
unset LD_PRELOAD
[[ \`\$PWD/sudo ls\` ]] && Su=Do
if [ dDo == d\$Su ];then
command="\$(pwd)/sudo "
command+="\$(pwd)/proot"
else
command="\$PWD/sudo "
command+="\$(pwd)/proot"
fi
command+=" --kill-on-exit"
command+=" --link2symlink"
command+=" -S alpine"
command+=""
command+=" -b alpine/root:/dev/shm"
## uncomment the following line to have access to the home directory of termux
#command+=" -b /data/data/com.termux/files/home:/root"
## uncomment the following line to mount /sdcard directly to /
command+=" -b /sdcard"
command+=" -w /root"
command+=" /usr/bin/env -i"
command+=" HOME=/root"
command+=" USER=root"
command+=" PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games"
command+=" TERM=xterm-256color"
command+=" LANG=C.UTF-8"
#command+=" /boot/init.sh"
#command+=" /etc/profile.d/motd.sh"
command+=" /bin/bash --login"
com="\$@"
if [ -z "\$1" ];then
    exec \$command
else
    \$command -c "\$com"
fi
EOF
#chmod 777 $(pwd)/ql.sh
echo "执行文件为: ql.sh""[ bash ql.sh ]"
#$(pwd)/sudo sh ql.sh || \
# && Sql=0
#if [ S0 == S$Sql ];then
sh ql.sh
#/data/user/0/bin.mt.plus/files/term/usr/bin/sudo bash ql.sh && cat
#$(pwd)/sudo sh ql.sh || /data/user/0/bin.mt.plus/files/term/usr/bin/sudo bash ql.sh
#[[ `/data/user/0/bin.mt.plus/files/term/usr/bin/sudo ls` ]] && /data/user/0/bin.mt.plus/files/term/usr/bin/sudo sh ql.sh || $(pwd)/sudo sh .ql.sh